package com.hzhr.iot.user.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.user.Global;
import com.hzhr.iot.user.constant.UserOptions;
import com.hzhr.iot.user.constant.User_ErrorsEnum;
import com.hzhr.iot.user.convertor.custom.ActionCustomConvertor;
import com.hzhr.iot.user.convertor.custom.ResourceCustomConvertor;
import com.hzhr.iot.user.convertor.custom.RoleCustomConvertor;
import com.hzhr.iot.user.convertor.custom.UserCustomConvertor;
import com.hzhr.iot.user.dao.*;
import com.hzhr.iot.user.dao.helper.CompanyDaoHelper;
import com.hzhr.iot.user.dao.helper.UserDaoHelper;
import com.hzhr.iot.user.dao.helper.UserInRoleDaoHelper;
import com.hzhr.iot.user.entity.UserEntity;
import com.hzhr.iot.user.entity.ext.UserEntityExt;
import com.hzhr.iot.user.model.user.UserGetModel;
import com.hzhr.iot.user.model.user_password.User_PasswordGetModel;
import com.hzhr.iot.user.model.user_password.User_PasswordModel;
import com.hzhr.iot.user.model.user_password.User_PasswordPutModel;
import com.hzhr.iot.user.queryModel.custom.UserCustomQueryModel;
import com.hzhr.iot.user.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = UserEntity.class, model = User_PasswordModel.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),

})
public class User_PasswordService extends BaseService {
    @Autowired
    UserCustomConvertor userCustomConvertor;
    @Autowired
    UserDao userDao;
    @Autowired
    User_PasswordService user_passwordService;

    //region javadoc注释 : get
    /**
     * 查询用户的密码 <br/><br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询用户的密码&nbsp;:&nbsp;(√) <br/>
     * 根据条件参数查询UserEntity对象user_1； <br/>
     * 2.&nbsp;返回信息&nbsp;:&nbsp;(√) <br/>
     * 根据user_1生成结果对象并返回; <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件 : where User_PasswordModel.id = #{equal_id};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @throws ServiceException <br/>
     * @see User_PasswordGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询用户的密码
    //步骤 :
    //1. 查询用户的密码 : (√)
    //根据条件参数查询UserEntity对象user_1；
    //2. 返回信息 : (√)
    //根据user_1生成结果对象并返回;
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where User_PasswordModel.id = #{equal_id}",required = true, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where User_PasswordModel.id = #{equal_id}",required = true, dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/users/passwords", method = RequestMethod.GET)
    //endregion
    public User_PasswordGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询用户 */
        //生成查询模型
        UserCustomQueryModel queryModel = userCustomConvertor.user_PasswordService_query2queryModel(query);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        //查询实体
        List<UserEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = userDao.selectExtList(queryModel);
        }
        //查询数量
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = userDao.selectCount(queryModel);
        }

        /* 返回信息 */
        User_PasswordGetModel result = userCustomConvertor.extList2user_PasswordGetModel(extList, total, null, query);
        return result;
    }

    //region javadoc注释 : putByUserId
    /**
     * 根据用户编号修改用户的密码 <br/><br/>
     * 步骤&nbsp;: <br/>
     * 1.&nbsp;查询用户&nbsp;:&nbsp;(√) <br/>
     * 根据以下筛选条件查询UserEntity对象user_1: <br/>
     * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1)&nbsp;user_1.id&nbsp;==&nbsp;路径参数userId <br/>
     * 如果user_1不存在，抛出异常201； <br/>
     * 2.&nbsp;修改用户密码&nbsp;:&nbsp;(√) <br/>
     * 根据以下规则修改user_1： <br/>
     * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(1)&nbsp;user_1&nbsp;=&nbsp;body.password； <br/>
     * 3.&nbsp;返回信息 <br/>
     * 查询修改后的user_1，并根据user_1、修改数量以及条件参数生成结果对象并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/><br/>
     * @param userId 用户编号 <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * USER_PASSWORDSERVICE_PUT_BY_USER_ID_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询用户<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 用户不存在<br/>
     * @see User_PasswordPutModel body <br/>
     * @see User_PasswordGetModel result <br/>
     */ 
    //endregion
    //region 说明 : putByUserId
    //根据用户编号修改用户的密码
    //步骤 :
    //1. 查询用户 : (√)
    //根据以下筛选条件查询UserEntity对象user_1:
    //          (1) user_1.id == 路径参数userId
    //如果user_1不存在，抛出异常201；
    //2. 修改用户密码 : (√)
    //根据以下规则修改user_1：
    //          (1) user_1 = body.password；
    //3. 返回信息
    //查询修改后的user_1，并根据user_1、修改数量以及条件参数生成结果对象并返回；
    //endregion
    //region 参数 : putByUserId
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "userId", value = "用户编号",required = true, dataType = "int", paramType = "path"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),
    })
    //endregion
    //region 请求 : putByUserId
    @RequestMapping(value = "/users/{userId}/passwords", method = RequestMethod.PUT)
    //endregion
    public User_PasswordGetModel putByUserId(Map<String, String> header, Integer userId,
                                             Map<String, Object> query, User_PasswordPutModel body) throws ServiceException {
        /* 查询用户 */
        UserCustomQueryModel queryModel = new UserCustomQueryModel();
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        queryModel.setEqual_id(userId);
        queryModel.setShow(true);
        UserEntity user_1 = userDao.selectEntity(queryModel);
        if (user_1 == null) {
            throw new ServiceException(User_ErrorsEnum.USER_PASSWORDSERVICE_PUT_BY_USER_ID_201);
        }

        /* 修改用户密码 */
        user_1.setPassword(body.getPassword());
        int changeCount = userDao.update(user_1);

        /* 返回信息 */
        query.put("_equal_id", user_1.getId());
        User_PasswordGetModel result = user_passwordService.get(initHeader(header, AccessModeEnum.GLOBAL), query);
        result.setChangeCount(changeCount);
        return result;
    }
}






































































































































































































































































































































































































